package com.bj.zspace.cat.controller;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author:liuwenqing
 * @Date:2024/12/10 20:03
 * @Description:
 **/
@RestController
public class DependencyTest {

    @GetMapping(value = "/dep/url")
    public String dependencyUrl() {
        Transaction t = Cat.newTransaction("URL", "hello");
        try {
            TimeUnit.SECONDS.sleep(1);
            t.complete();
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.setStatus(Message.SUCCESS);
            t.complete();
        }
        return "ok";

    }

    @GetMapping(value = "/dep/cache")
    public String depCache() {
        Transaction t = Cat.newTransaction("Cache.user", "user");
        try {
            TimeUnit.SECONDS.sleep(1);
            t.complete();
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.setStatus(Message.SUCCESS);
            t.complete();
        }
        return "ok";

    }

    @GetMapping(value = "/dep/sql")
    public String depSql() {
        Transaction t = Cat.newTransaction("SQL", "update");
        try {
            Cat.logEvent("SQL.Database", "jdbc:mysql://10.2.57.60:3306/cat_db");
            TimeUnit.SECONDS.sleep(1);
            t.complete();
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.setStatus(Message.SUCCESS);
            t.complete();
        }
        return "ok";
    }

    @GetMapping(value = "dep/pigeonCall")
    public String depPigeonCall() {
        Transaction t = Cat.newTransaction("PigeonCall", "findUser");
        try {
            Cat.logEvent("PigeonCall.app", "UserService");
            TimeUnit.SECONDS.sleep(1);
            t.complete();
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.setStatus(Message.SUCCESS);
            t.complete();
        }

        return "ok";

    }

}
